import {getAddressList} from '../../assets/js/request.js'

const state = {
    addressList:JSON.parse(localStorage.getItem('addressList')) || [],
    // defaultAddress:JSON.parse(localStorage.getItem('defaultAddress')) || {},

};
const mutations = {
    setAddressList(state,data) {
        state.addressList = data
        console.log(state.addressList);
    },
    // setDefaultAddress(state,data) {
    //     state.defaultAddress = data
    // }
};
const actions = {
    // 获取地址列表
    async getAddress({commit}) {
        let addressList =  await getAddressList();
        console.log(addressList);
        commit('setAddressList',addressList);
    }, 
    // async getDefaultAddress({commit}) {
    //     let defaultAddress = await getDefaultAddress();
    //     console.log(defaultAddress);
    //     commit('setDefaultAddress',defaultAddress )
    // }
    

};
const getters = {
    noDefaultAddress(state) {
        if(state.addressList.length > 0) {
            return state.addressList.filter(ele => ele.default_set === "0")
            // return a
        }else {
            return []
        }
    },
    defaultAddress(state) {
        if(state.addressList.length > 0) {
            return state.addressList.filter(ele=>ele.default_set === "1")
        }else {
            return []
        }
    }
};
export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters,
}